//
//  MYMySetRevampVC.m
//  MuYing
//
//  Created by tbb on 2019/8/8.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYMySetRevampVC.h"
#import "MYMySetRevampPasswordVC.h"
@interface MYMySetRevampVC ()
@property (nonatomic, strong) UIButton * codeButton;
@property (nonatomic, strong) Calculagraph *codeTimer;
@end

@implementation MYMySetRevampVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isNavShadow = YES;
    self.navigationItem.title = kYQLS(@"修改密码");
    self.rightItemTitle = kYQLS(@"下一步");

}
-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    kYQREGISTERTABLEVIEWCELL([MYMySetRevampCodeCell class])
    kYQREGISTERTABLEVIEWCNIBHeaderFooter(@"MYMySetRevampFooterView")
    
}

-(void)rightItemAction:(UIButton *)sender {
    [self.view endEditing:true];
    MYMySetRevampCodeCell *cell = (MYMySetRevampCodeCell *)[self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    NSString *text = cell.textField.text;
    if ([text removeStringSpace].length != 6) {
        [MBProgressHUD showMessage:kYQLS(@"请输入验证码")];
//        [QMUITips showError:@"请输入正确的验证码" inView:self.view hideAfterDelay:1];
        return;
    }
    
    MYMySetRevampPasswordVC *passwordVC = [[MYMySetRevampPasswordVC alloc] init];
    kYQPUSH(passwordVC);
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MYMySetRevampCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYMySetRevampCodeCell"];
    cell.placeholderStr = kYQLS(@"请输入验证码");
    [cell.codeBtn addTarget:self action:@selector(codeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.codeButton = cell.codeBtn;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    MYMySetRevampFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYMySetRevampFooterView"];
    
    NSString *yjRegisterProtocolStr = [NSString stringWithFormat:@" +86 %@",[YQPUserTool account].mobile?:@"158****9363"];
    NSString *text = [kYQLS(@"我们将验证码发送至手机号") stringByAppendingString:yjRegisterProtocolStr];
    NSMutableAttributedString *addTagStr = [[NSMutableAttributedString alloc] initWithString:text];
    
    NSRange range = [text rangeOfString:yjRegisterProtocolStr];
    [addTagStr setColor:kAPPThemeColor range:range];
    
    footerView.textLb.attributedText = [addTagStr copy];
    
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 24;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

#pragma mark - 获取短信验证码
- (void)codeBtnClick
{
    MYMySetRevampCodeCell *codeCell = [self.listTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    NSString *url = kGET_CODE_SMS_URL;
    NSMutableDictionary *dic = NSMutableDictionary.new;

    dic[@"phone"] = [YQPUserTool account].mobile;

    if ([YQPUserTool account].mobile.length == 0) {
        [MBProgressHUD showSuccess:kYQLS(@"请绑定手机号")];
        return;
    }

    [YQPNetWorkingManager sendGetRequest:url parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        [MBProgressHUD showSuccess:kYQLS(@"发送成功")];
        [self.codeTimer start];
        //codeCell.textField.text = responseObject.data;
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        
    }];
}

#pragma mark 计时器KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"time"]) {
        NSInteger seconds = [[change objectForKey:@"new"] integerValue];
        NSString *time = [NSString stringWithFormat:@"%zdS",60-seconds];
        self.codeButton.enabled = NO;
        [self.codeButton setTitle:time forState:UIControlStateDisabled];
        
        if ([time isEqualToString:@"0S"]) {
            [self.codeTimer stop];
            self.codeButton.enabled = YES;
            [self.codeButton setTitle:kYQLS(@"重新获取") forState:(UIControlStateNormal)];
            [self.listTableView reloadData];
        }
    }
}

#pragma mark-- lazy
- (Calculagraph *)codeTimer {
    if (!_codeTimer) {
        _codeTimer = [[Calculagraph alloc] init];
        [_codeTimer addObserver:self forKeyPath:@"time" options:NSKeyValueObservingOptionNew context:nil];
    }
    return _codeTimer;
}
#pragma mark-- lazy
- (void)dealloc {
    [self.codeTimer removeObserver:self forKeyPath:@"time"];
}
@end

